<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="起点" prop="startPoint">
        <el-input
          v-model="queryParams.startPoint"
          placeholder="请输入起点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终点" prop="endPoint">
        <el-input
          v-model="queryParams.endPoint"
          placeholder="请输入终点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物名称" prop="cargoName">
        <el-input
          v-model="queryParams.cargoName"
          placeholder="请输入货物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="装货时间" prop="loadingTime">
        <el-date-picker clearable
                        v-model="queryParams.loadingTime"
                        type="date"
                        value-format="yyyy-MM-dd hh-mm-ss"
                        placeholder="请选择装货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wuliu:message:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wuliu:message:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wuliu:message:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wuliu:message:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="起点" align="center" prop="startPoint"/>
      <el-table-column label="终点" align="center" prop="endPoint"/>
      <el-table-column label="货物名称" align="center" prop="cargoName"/>
      <el-table-column label="体积" align="center" prop="volume"/>
      <el-table-column label="重量" align="center" prop="weight"/>
      <el-table-column label="运费" align="center" prop="freight"/>
      <el-table-column label="装货时间" align="center" prop="loadingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loadingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="telephone"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="是否推荐">
        <template slot-scope="scope">
          <template v-for="option in recommendedOptions">
            <el-tag size="small" v-if="option.value===scope.row.recommended" :type="option.type">{{
              option.label
              }}
            </el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleComplete(scope.row)"
            v-hasPermi="['wuliu:message:edit']"
          >完成
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleOrder(scope.row)"
            v-hasPermi="['wuliu:message:edit']"
          >抢单
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wuliu:message:edit']"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物流信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="起点" prop="startPoint">
          <el-input v-model="form.startPoint" placeholder="请输入起点" maxLength='36'/>
        </el-form-item>
        <el-form-item label="终点" prop="endPoint">
          <el-input v-model="form.endPoint" placeholder="请输入终点" maxLength='36'/>
        </el-form-item>
        <el-form-item label="货物名称" prop="cargoName">
          <el-input v-model="form.cargoName" placeholder="请输入货物名称" maxLength='36'/>
        </el-form-item>
        <el-form-item label="体积" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入体积" oninput="value=value.replace(/[^\d.]/g,'')"
                    maxLength='9'/>
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" oninput="value=value.replace(/[^\d.]/g,'')"
                    maxLength='9'/>
        </el-form-item>
        <el-form-item label="运费" prop="freight">
          <el-input v-model="form.freight" placeholder="请输入运费" oninput="value=value.replace(/[^\d.]/g,'')"
                    maxLength='9'/>
        </el-form-item>
        <el-form-item label="装货时间" prop="loadingTime">
          <el-date-picker clearable
                          v-model="form.loadingTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择装货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话" oninput="value=value.replace(/[^\d.]/g,'')"
                    maxLength='11'/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="是否推荐" prop="recommended">
          <template>
            <el-radio v-model="form.recommended" :label="1">推荐</el-radio>
            <el-radio v-model="form.recommended" :label="2">不推荐</el-radio>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改物流信息对话框 -->
    <el-dialog title="抢单列表" :close-on-click-modal="false" :visible.sync="openOrder" width="900px" append-to-body>
      <el-table :data="orderUserList" v-loading="orderLoading">
        <el-table-column type="index" width="60" label="序号"/>
        <el-table-column prop="userName" label="姓名"/>
        <el-table-column prop="telephone" label="电话"/>
        <el-table-column prop="carType" label="车辆属性"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <template v-for="option in orderStatusOptions">
              <el-tag size="small" v-if="option.value===scope.row.status" :type="option.type">{{
                  option.label
                }}
              </el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status==='6'"
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handleOrderStatus(scope.row,1)"
            >派单
            </el-button>
            <el-button
              v-if="scope.row.status==='6'"
              size="mini"
              type="text"
              icon="el-icon-close"
              @click="handleOrderStatus(scope.row,0)"
            >拒绝
            </el-button>
            <el-button
              v-if="scope.row.status==='3'"
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handleOrderStatus(scope.row,4)"
            >完成
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMessage,
  getMessage,
  delMessage,
  addMessage,
  updateMessage,
  getOrderUserList, putOrderStatus
} from "@/api/wuliu/message";

export default {
  name: "Message",
  dicts: ['order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流信息表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //抢单弹出层
      openOrder: false,
      //抢单加载
      orderLoading: false,
      //抢单人员数据
      orderUserList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startPoint: null,
        endPoint: null,
        cargoName: null,
        volume: null,
        weight: null,
        freight: null,
        loadingTime: null,
        telephone: null,
        status: null,
        completionTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startPoint: [
          {required: true, message: "起点不能为空", trigger: "blur"}
        ],
        endPoint: [
          {required: true, message: "终点不能为空", trigger: "blur"}
        ],
        cargoName: [
          {required: true, message: "货物名称不能为空", trigger: "blur"}
        ],
        freight: [
          {required: true, message: "运费不能为空", trigger: "blur"}
        ],
        loadingTime: [
          {required: true, message: "装货时间不能为空", trigger: "blur"}
        ],
        telephone: [
          {required: true, message: "联系电话不能为空", trigger: "blur"}
        ],
      },
      orderStatusOptions: [
        //0未中标1中标2已装货3已卸货4完成5撤销订单6接单中
        {
          value: '0',
          label: '未中标',
          type: 'warning'
        },
        {
          value: '1',
          label: '中标',
          type: 'success'
        },
        {
          value: '2',
          label: '已装货',
          type: 'success'
        },
        {
          value: '3',
          label: '已卸货',
          type: 'success'
        },
        {
          value: '4',
          label: '完成',
          type: 'success'
        },
        {
          value: '5',
          label: '撤销订单',
          type: 'warning'
        },
        {
          value: '6',
          label: '接单中',
          type: ''
        }
      ],
      recommendedOptions: [
        {
          value: '1',
          label: '推荐',
          type: 'success'
        },
        {
          value: '2',
          label: '不推荐',
          type: 'success'
        }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物流信息列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        startPoint: null,
        endPoint: null,
        cargoName: null,
        volume: null,
        weight: null,
        freight: null,
        loadingTime: null,
        telephone: null,
        status: null,
        remark: null,
        createTime: null,
        updateTime: null,
        completionTime: null,
        recommended: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.status == null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物流信息";
      });
    },
    //点击抢单按钮
    handleOrder(row) {
      this.openOrder = true;
      this._orderRowParams = row;
      this.getOrderData()
    },
    async getOrderData() {
      this.orderLoading = true;
      this.orderUserList = [];
      const res = await getOrderUserList(this._orderRowParams.id);
      this.orderLoading = false;
      if (res.code === 200) {
        this.orderUserList = res.data;
      }
    },
    //点击抢单列表 操作
    async handleOrderStatus(row, status) {
      const res = await putOrderStatus({id: row.userOrderId, status});
      if (res.code === 200) {
        this.getOrderData();
        this.$message.success('操作成功')
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (this.form.volume == null && this.form.weight == null){
          this.$modal.msgError("体积和重量必须选择一项填写");
          return;
        }
        if (valid) {
          if (this.form.id != null) {
            updateMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物流信息编号为"' + ids + '"的数据项？').then(function () {
        return delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 完成按钮操作 */
    handleComplete(row) {
      this.$modal.confirm('是否确认完成该订单？').then(function () {
        return updateMessage({id: row.id, status: 2});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wuliu/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
